/*************************************************
Author: will
Date: 9/11/2012
Description:
Notes: 
*************************************************/

#ifndef __CValidTask_h__
#define __CValidTask_h__

#include "CTArray.h"
#include "CTaskDef.h"
#include "CTaskID.h"
#include "CTaskTargetStat.h"

class CTaskMgr;
class CUserTask;
class CValidTask
{
    friend class CUserTask;
    friend class CTaskMgr;
    
    enum TASK_STAT 
    {
        TASK_STAT_NORMAIL = 0,  //进行中
        TASK_STAT_FINISH,       //完成
        TASK_STAT_FAIL,         //失败
    };

public:
	CValidTask() 
    {
        _ucTaskStat = 0;
        _iTaskEndTime = 0; 
        _uiPublishGID = 0;
	}

    CValidTask(CTaskID stTaskID)
        :_stTaskID(stTaskID)
    {
        _ucTaskStat = 0;
        _iTaskEndTime = 0; 
        _uiPublishGID = 0;
    }

	virtual ~CValidTask() 
    {
	}

    int Init(CValidTaskData& stData)
    {
        int iRet = 0;

        _ucTaskStat = stData._ucIsFinished;

        iRet = _stTaskID.Init(stData._stTaskID);
        CHECK_RETURN(iRet);

        _astTargetStats.Clear();
        for (int i = 0; i < stData._astTargetStats.Size(); ++i)
        {
            CTaskTargetStat stTargetStat;
            iRet = stTargetStat.Init(stData._astTargetStats[i]);
            CHECK_RETURN(iRet);

            _astTargetStats.AddOneItem(stTargetStat);
        }

        _iTaskEndTime = stData._iTaskEndTime;
        _uiPublishGID = stData._uiPublishGID;

        return 0;
    }

    int GetData(CValidTaskData& stData)
    {
        int iRet = 0;

        stData._ucIsFinished = _ucTaskStat;

        iRet = _stTaskID.GetData(stData._stTaskID);
        CHECK_RETURN(iRet);

        stData._astTargetStats.Clear();
        for (int i = 0; i < _astTargetStats.Size(); ++i)
        {
            CTaskTargetStatData stTargetStatData;
            iRet = _astTargetStats[i].GetData(stTargetStatData);
            CHECK_RETURN(iRet);

            stData._astTargetStats.AddOneItem(stTargetStatData);
        }

        stData._iTaskEndTime = _iTaskEndTime;
        stData._uiPublishGID = _uiPublishGID;

        return 0;
    }

    bool IsFinished()
    {
        return _ucTaskStat == TASK_STAT_FINISH;
    }

    int CheckFinished()
    {
        if (IsFinished())
        {
            return 0;
        }

        for (int i = 0; i < _astTargetStats.Size(); ++i)
        {
            if (!_astTargetStats[i].IsReachCond())
            {
                return 0;
            }
        }
        
        _ucTaskStat = TASK_STAT_FINISH;
        return 0;
    }

    CTaskID & GetTaskID()
    {
        return this->_stTaskID;
    }

private:
    unsigned char    _ucTaskStat;
	CTaskID _stTaskID;
    CTLib::CTArray<CTaskTargetStat, MAX_TASK_FINISH_COND>   _astTargetStats;
    int             _iTaskEndTime;  
    unsigned int    _uiPublishGID;
};


#endif // __CValidTask_h__

